<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>layui</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <link rel="stylesheet" href="../layui.css">
</head>
<body>        
<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
</fieldset>
<form class="layui-form" action="/admin/user/save" method="post">
<div class="layui-col-md10">
      <div class="layui-form-item">
    <div class ="layui-inline">
    <label class="layui-form-label">人员编号</label>
      <div class="layui-input-inline">
        <input type="text" name="id" required  lay-verify="required"  autocomplete="off" class="layui-input id" disabled="disabled">
      </div>
    </div>
    <div class ="layui-inline">
    <label class="layui-form-label">职位</label>
      <div class="layui-input-inline">
        <input type="text" name="role" required  lay-verify="required"  autocomplete="off" class="layui-input role" disabled="disabled">
      </div>
    </div>
  </div>
    <div class="layui-form-item">
    <div class ="layui-inline">
    <label class="layui-form-label">姓名</label>
      <div class="layui-input-inline">
        <input type="text" name="tName" required  lay-verify="required"  autocomplete="off" class="layui-input tName" disabled="disabled">
      </div>
    </div>
    <div class ="layui-inline">
    <label class="layui-form-label">工号</label>
      <div class="layui-input-inline">
        <input type="text" name="tSerial" required  lay-verify="required"  autocomplete="off" class="layui-input tSerial" disabled="disabled">
      </div>
    </div>
  </div>
    <div class="layui-form-item">
    <div class ="layui-inline">
    <label class="layui-form-label">所在学院</label>
      <div class="layui-input-inline">
        <input type="text" name="colleage" required  lay-verify="required"  autocomplete="off" class="layui-input colleage " disabled="disabled">
      </div>
    </div>
    <div class ="layui-inline">
    <label class="layui-form-label">所在系</label>
      <div class="layui-input-inline">
        <input type="text" name="department" required  lay-verify="required"  autocomplete="off" class="layui-input department" disabled="disabled">
      </div>
    </div>
  </div>
    <div class="layui-form-item">
    <div class ="layui-inline">
    <label class="layui-form-label">联系方式</label>
      <div class="layui-input-inline">
        <input type="text" name="telephone" required  lay-verify="required" placeholder="请输入联系方式" autocomplete="off" class="layui-input telephone">
      </div>
    </div>
    <div class ="layui-inline">
      <div class="layui-input-inline">
        <input type="text" name="password" required  lay-verify="required"  autocomplete="off" class="layui-input password" style="display: none;" id="pwded">
      </div>
    </div>
  </div>
  <!-- 留一个修改密码的div -->
  <div id="changePassword"></div>
  <div class="layui-form-item">
    <div class="layui-input-block">
      <button class="layui-btn" lay-submit lay-filter="formDemo">保存</button>
      <button type="reset" class="layui-btn layui-btn-primary">重置</button>
      <button class="layui-btn layui-btn-primary" onclick="change()">修改密码</button>
      <button class="layui-btn" onclick="login(123)">模拟登陆结果</button>
    </div>
  </div>
 </div>
 <div class="layui-col-md2">
    <div class="layui-upload">
      <div class="layui-upload-list">
        <img class="layui-upload-img" id="demo1" name="photo">
        <p id="demoText"></p>
      </div>
      <button type="button" class="layui-btn" id="test1" style="margin-bottom: 50px">上传图片</button>
  </div>
</div>
</form>

<script src="../layui.js" charset="utf-8"></script>

<script src="../jquery.min.js"></script>
<script>

function change(){
  document.getElementById("changePassword").innerHTML="<div class = 'layui-form-item'>"
  +"<div class='layui-inline'>"
  +"<label class='layui-form-label'>原始密码</label>"
  +"<div class='layui-input-inline'>"
  +"<input type='text' required lay-verify='required' placeholder='请输入原始密码' autocomplete='off' class='layui-input' id='pword' onchange='checkPassword()'>"
  +"</div>"
  +"<div class='layui-inline'>"
  +"<label class='layui-form-label'>新密码</label>"
  +"<div class='layui-input-inline'>"
  +"<input type='password' required lay-verify='required' placeholder='请输入新密码' autocomplete='off' class='layui-input' id='pword1'>"
  +"</div>"
  +"</div>"
  +"<div class='layui-inline'>"
  +"<label class='layui-form-label'>确认一次</label>"
  +"<div class='layui-input-inline'>"
  +"<input type='password' name ='password' required lay-verify='required' placeholder='确认一次' autocomplete='off' class='layui-input password' id='pword2' onchange='check()'>"
  +"</div>"
  +"<div class='layui-form-mid layui-word-aux' >"
  +"<span id='tishi'></span>"
  +"</div>"
  +"</div>"
  +"</div>";

}

function checkPassword(){
  var ypwd = document.getElementById("pword").value;
  var npwd = document.getElementById("pwded").value;
  if(ypwd!=npwd){
    document.getElementById("tishi").innerHTML="<font color='red'>原始密码错误</font>";
  }else{
    document.getElementById("tishi").innerHTML="";
  }
}
function check(){
      var pwd1 = document.getElementById("pword1").value;
      var pwd2 = document.getElementById("pword2").value;
      if(pwd1 == pwd2) {
                  document.getElementById("tishi").innerHTML="<font color='green'>两次密码相同</font>";
              }
              else {
                  document.getElementById("tishi").innerHTML="<font color='red'>两次密码不相同</font>";
              }
}
function login(tSerial){
  $.ajax({
    url:"/admin/user/login/"+tSerial,
    type:"post",
    dataType:"json",
    data:{},
    success:function(result){
      $(".id").val(result.data.id);
      $(".role").val(result.data.role);
      $(".tName").val(result.data.tName);
      $(".tSerial").val(result.data.tSerial);
      $(".colleage").val(result.data.colleage);
      $(".department").val(result.data.department);
      $(".telephone").val(result.data.telephone);
      $(".password").val(result.data.password);
      console.log(result.data.tSerial);
    },
    error:function(result){
      console.log("sb");
    }
  })
}

layui.use(['form', 'layedit', 'laydate','upload'], function(){
  var form = layui.form
  ,layer = layui.layer
  ,layedit = layui.layedit
  ,upload = layui.upload
  ,laydate = layui.laydate;
  
  var $ =layui.jquery;

  //普通图片上传
  var uploadInst = upload.render({
    elem: '#test1'
    ,url: '/upload/'
    ,before: function(obj){
      //预读本地文件示例，不支持ie8
      obj.preview(function(index, photo, result){
        $('#demo1').attr('src', result); //图片链接（base64）
      });
    }
    ,done: function(res){
      //如果上传失败
      if(res.code > 0){
        return layer.msg('上传失败');
      }
      //上传成功
    }
    ,error: function(){
      //演示失败状态，并实现重传
      var demoText = $('#demoText');
      demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-mini demo-reload">重试</a>');
      demoText.find('.demo-reload').on('click', function(){
        uploadInst.upload();
      });
    }
  });


  //日期
  laydate.render({
    elem: '#date'
  });
  //创建一个编辑器
  var editIndex = layedit.build('LAY_demo_editor');
 
  //自定义验证规则

  //以后要留下的验证区域



  form.verify({
    title: function(value){
      if(value.length < 5){
        return '标题至少得5个字符啊';
      }
    }
    ,pass: [/(.+){6,12}$/, '密码必须6到12位']
    ,content: function(value){
      layedit.sync(editIndex);
    }
  });

  
  //监听提交
  
  form.on('submit(formDemo)', function(data){
    console.log(data.field);
    var jhxhr = $.ajax({
      'url': data.form.action,
      'data': data.field,
      'type': 'POST'
    });
    
    jhxhr.done(function(result) {
     alert("保存成功");  
    });

    return false;
  });
});
</script>

</body>
</html>